package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import jsdai.lang.SdaiException;

import spfRead.ReadSpf;

public class Window1 implements ActionListener  {

	private JPanel contentPane;
	private JTextField W1BrwTextField;
	private JTextField W1SelcTextField;

	private JFrame win1;

	public JButton W1btnBrowse;
	public JButton W1btnSelect;

	public JButton W1btnOk;
	public JButton W1btnCancel;
	
	JMenuItem mntmVersion;

	JMenuItem mntmHelp;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Window1 frame = new Window1();
					frame.win1.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Window1() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		win1=new JFrame();
		win1.setTitle("window1 : Express spf ->Express XML");
		win1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		win1.setBounds(100, 100, 450, 289);

		JMenuBar menuBar = new JMenuBar();
		win1.setJMenuBar(menuBar);

		JMenu mnInformation = new JMenu("Information");
		menuBar.add(mnInformation);

		this.mntmVersion = new JMenuItem("Version");
		mnInformation.add(this.mntmVersion);
		this.mntmVersion.addActionListener(this);
		
		this.mntmHelp = new JMenuItem("Help");
		menuBar.add(this.mntmHelp);
		this.mntmHelp.addActionListener(this);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		win1.setContentPane(contentPane);

		JPanel panel = new JPanel();
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
				gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 432, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
				);
		gl_contentPane.setVerticalGroup(
				gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
						.addContainerGap()
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 224, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(18, Short.MAX_VALUE))
				);

		JLabel lblExpressIleexp = new JLabel("Express file (.spf)");

		this.W1BrwTextField = new JTextField();
		this.W1BrwTextField.setColumns(10);

		this.W1btnBrowse = new JButton("Browse");

		JLabel lblDeposit = new JLabel("Deposit");

		this.W1SelcTextField = new JTextField(System.getProperty("user.dir"));
		this.W1SelcTextField.setColumns(10);

		this.W1btnSelect = new JButton("Select");

		this.W1btnOk = new JButton("OK");

		this.W1btnCancel = new JButton("Cancel");

		//ajouter les listener
		this.W1btnBrowse.addActionListener(this);
		this.W1btnCancel.addActionListener(this);
		this.W1btnOk.addActionListener(this);
		this.W1btnSelect.addActionListener(this);

		GroupLayout gl_panel = new GroupLayout(panel);

		gl_panel.setHorizontalGroup(
				gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
						.addContainerGap()
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel.createSequentialGroup()
										.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
												.addComponent(lblExpressIleexp)
												.addComponent(lblDeposit)
												.addGroup(gl_panel.createSequentialGroup()
														.addGap(12)
														.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
																.addComponent(this.W1SelcTextField, Alignment.LEADING)
																.addComponent(this.W1BrwTextField, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE))
																.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
																.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
																		.addComponent(this.W1btnSelect)
																		.addComponent(this.W1btnBrowse))))
																		.addContainerGap())
																		.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
																				.addComponent(this.W1btnOk)
																				.addPreferredGap(ComponentPlacement.UNRELATED)
																				.addComponent(this.W1btnCancel)
																				.addGap(17))))
				);
		gl_panel.setVerticalGroup(
				gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
						.addContainerGap()
						.addComponent(lblExpressIleexp)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(this.W1BrwTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(this.W1btnBrowse))
								.addGap(18)
								.addComponent(lblDeposit)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
										.addComponent(this.W1SelcTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(this.W1btnSelect))
										.addPreferredGap(ComponentPlacement.RELATED, 59, Short.MAX_VALUE)
										.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
												.addComponent(this.W1btnOk)
												.addComponent(this.W1btnCancel))
												.addGap(59))
				);
		panel.setLayout(gl_panel);
		contentPane.setLayout(gl_contentPane);
	}

	public void actionPerformed(ActionEvent e){
		Object s=e.getSource();
		if (s==this.W1btnCancel){
			this.win1.dispose();
		}else if (s==this.W1btnBrowse){
			JFileChooser fc=new JFileChooser();
			fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
			int returnval=fc.showOpenDialog(fc);
			if (returnval==JFileChooser.APPROVE_OPTION){
				File f=fc.getSelectedFile();
				this.W1BrwTextField.setText(f.getAbsolutePath());
				System.out.println("fichier choisi =" +f.getName());
			}
		}else if (s==this.W1btnSelect){
			JFileChooser fc=new JFileChooser();
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			int returnval=fc.showOpenDialog(fc);
			if (returnval==JFileChooser.APPROVE_OPTION){
				File f=fc.getSelectedFile();
				this.W1SelcTextField.setText(f.getAbsolutePath());
				System.out.println("dossier choisi =" +f.getName());
			}
		}else if(s==this.W1btnOk){
			String spfPath=this.W1BrwTextField.getText();
			if(spfPath!=null){
				File spfFile=new File(spfPath);
				if(!spfFile.exists()){
					JOptionPane.showMessageDialog(null,"the spf does not exist!", "Error",JOptionPane.ERROR_MESSAGE);
				}else{
					String dp=this.W1SelcTextField.getText();
					dp=dp+"/"+spfFile.getName().substring(0,spfFile.getName().indexOf('.'))+".xml";
					System.out.println("sorti =" +dp);
					String[] args={spfPath, dp};
					try {
						ReadSpf.main(args);
					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (SdaiException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IOException e2){
						e2.printStackTrace();
					}
					this.win1.dispose();
				}
			}else{
				JOptionPane.showMessageDialog(null,"Indicate an spf please", "Error",JOptionPane.ERROR_MESSAGE);
			}
		}else 		if(s==this.mntmVersion){
			
			Version.main(null);
		}
		else if(s==this.mntmHelp){
			Help.main(null);
		}
	}

}
